package com.yang.wechat.service.impl;

import com.yang.wechat.common.MessageUtil;
import com.yang.wechat.pojo.ImageMessageReply;
import com.yang.wechat.pojo.MediaId;
import com.yang.wechat.pojo.TextMessage;
import com.yang.wechat.pojo.VoiceMessageReply;
import com.yang.wechat.service.WeChatService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created with Administrator
 * DATE:2017/3/8
 * Time:15:54
 */
@Service
public class WeChatServiceImpl implements WeChatService {

    private static Logger logger = Logger.getLogger(WeChatServiceImpl.class);

    public String weixinPost(HttpServletRequest request) {
        String respMessage = "success";
        try {
            Map<String, String> requestMap = MessageUtil.xmlToMap(request);
            assert requestMap != null;
            String fromUserName = requestMap.get("FromUserName");
            String toUserName = requestMap.get("ToUserName");
            String msgType = requestMap.get("MsgType");
            String content = requestMap.get("Content");
            String picUrl = requestMap.get("PicUrl");//图片链接（由系统生成）
            String mediaId = requestMap.get("MediaId");//图片消息媒体id，可以调用多媒体文件下载接口拉取数据。
            logger.info("FromUserName is :" + fromUserName + "\nToUserName is :" + toUserName + "\nmsgType is :" + msgType + "\npicUrl is " + picUrl + "\nmediaId is" + mediaId);

            if (msgType.equals(MessageUtil.RESP_MESSAGE_TYPE_TEXT)) {
                TextMessage textMessage = new TextMessage();
                textMessage.setContent("the text is :" + content);
                textMessage.setToUserName(fromUserName);
                textMessage.setFromUserName(toUserName);
                textMessage.setCreateTime(new Date().getTime() + "");
                textMessage.setMsgType(msgType);
                respMessage = MessageUtil.textMessageToXml(textMessage);
                logger.info("respMessage:" + respMessage);
            } else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
                String eventType = requestMap.get("Event");
                if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
                    TextMessage text = new TextMessage();
                    text.setContent("欢迎关注，我在这里等候多时了~");
                    text.setToUserName(fromUserName);
                    text.setFromUserName(toUserName);
                    text.setCreateTime(new Date().getTime() + "");
                    text.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
                    respMessage = MessageUtil.textMessageToXml(text);
                    logger.info("respMessage:" + respMessage);
                }
            } else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
                ImageMessageReply imageMessageReply = new ImageMessageReply();
                MediaId mid = new MediaId();
                mid.setMediaId(mediaId);
                imageMessageReply.setImage(mid);
                imageMessageReply.setCreateTime(new Date().getTime() + "");
                imageMessageReply.setFromUserName(toUserName);
                imageMessageReply.setToUserName(fromUserName);
                imageMessageReply.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
                respMessage = MessageUtil.imageMessageToXml(imageMessageReply);
                logger.info("respMessage:" + respMessage);

            } else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
                VoiceMessageReply voiceMessageReply = new VoiceMessageReply();
                MediaId voiceId = new MediaId();
                voiceId.setMediaId(mediaId);
                voiceMessageReply.setVoice(voiceId);
                voiceMessageReply.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_VOICE);
                voiceMessageReply.setCreateTime(new Date().getTime() + "");
                voiceMessageReply.setFromUserName(toUserName);
                voiceMessageReply.setToUserName(fromUserName);
                respMessage = MessageUtil.voiceMessageToXml(voiceMessageReply);
                logger.info("respMessage:" + respMessage);
            }

        } catch (Exception e) {
            logger.error("weixinPost error :" + e.getMessage());
            e.printStackTrace();
            respMessage = "success";
        }
        return respMessage;
    }
}